import java.io.*;
import java.util.ArrayList;


class Parser {
	
	public Node parse(String file){
		Node mother = new Node();
		Node active = mother;
		
		try{
			FileInputStream fstream = new FileInputStream(file);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
	
			String tempString = "a";
			char characterRead = 'a';
			char nextCharacterRead = 'a';
			int intRead = br.read();
			
			boolean parsingTag = false;
			boolean closingTag = false;
			boolean parsingQuote = false;
	
			while (intRead != -1) {
				characterRead = (char)intRead;
				
				if(closingTag){
					if(characterRead == '>'){
						closingTag = false;
						active = active.parent;
					}
				}
				
				else if(parsingTag){
					if(characterRead == '>'){
						parsingTag = false;
						active.add(tempString);
						tempString = "";
					}
					else if(characterRead == '"') parsingQuote = !parsingQuote;
					else if((characterRead == ' ' || characterRead == '=') && !parsingQuote){
						active.add(tempString);
						tempString = "";
					}
					else if(characterRead != '\n') tempString += characterRead;
				}
				
				else if(characterRead == '<'){
					active.phrase += tempString.trim();
					tempString = "";
					nextCharacterRead = (char)br.read();
					if(nextCharacterRead == '/') closingTag = true;
					else{
						parsingTag = true;
						if(nextCharacterRead != '\n') tempString += nextCharacterRead;
						Node newChild = new Node(active);
						active.add(newChild);
						active = newChild;
					}
				}
			
				else tempString += characterRead;
				
				intRead = br.read();
			}

			in.close();
		}
		
		catch (Exception e){
			System.err.println("Error: " + e.getMessage());
		}
		
		if(mother.nbChildren() == 1) return mother.children.get(0);
		else return mother;
	}
	
	
}